﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Vision.Common
{
    //A*x+B*y+C=0
    class CDrawLine:CDrawObj
    {
        #region 数据
        double A = 0;
        double B = 0;
        double C = 0;
        public double x1 = 0;
        public double y1 = 0;
        public double x2 = 0;
        public double y2 = 0;
        #endregion

        #region 绘制
        public override void Draw(Graphics g, float scale = 1)
        {
            if (m_visible == false)
                return;
            if (scale > 1)
                scale = 1;
            g.DrawLine(new Pen(Color.Red), (float)x1, (float)y1, (float)x2, (float)y2);
        }
        #endregion
        public CDrawLine(double x1=0, double y1=0, double x2=0, double y2=0)
        {
            Create(x1, y1, x2, y2);
        }

        public void  Create(double x1 = 0, double y1 = 0, double x2 = 0, double y2 = 0)
        {
            this.x1 = x1; this.y1 = y1;
            this.x2 = x2; this.y2 = y2;
            A = y2 - y1;
            B = x1 - x2;
            C = x2 * y1 - x1 * y2;
        }
        public void CreateByLine2D(double vx,double vy,double x1,double y1)
        {
            this.x1 = x1;
            this.y1 = y1;
            A = vy / vx;
            B = -1;
            C = -A * x1 + y1;
            UpdateEndByY(y1 + 10);
        }
        public double GetDistance(double x,double y)
        {
            double dis = (A * x + B * y + C) / Math.Sqrt(A * A + B * B);
            return dis;
        }
        public void UpdateStartByY(double y)
        {
            this.y1 = y;
            if (A !=0)
                x1 = (float)-(B * y + C) / A;
            else
                x1 = 0;
        }
        public void UpdateEndByY(double y)
        {
            this.y2 = y;
            if (A != 0)
                x2 = -(B * y + C) / A;
            else
                x2 = 0;
        }

    }
}
